package com.rattsoftware.server.dao.hibernate;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.googlecode.genericdao.search.Search;
import com.googlecode.genericdao.search.SearchResult;
import com.rattsoftware.server.admin.model.CourseUnit;
import com.rattsoftware.server.admin.model.paging.PagingLoadConfig;
import com.rattsoftware.server.admin.model.paging.PagingLoadResult;
import com.rattsoftware.server.dao.CourseUnitDAO;

/**
 * Provides a hibernate implementation
 * of the <code>CourseUnitDAO</code> data access <code> interface.</code>
 */
@Repository("courseUnitDAO")
public class HibernateCourseUnitDAO extends BaseDAOImpl<CourseUnit> implements CourseUnitDAO {

	@Override
	public void deleteCourseUnit(CourseUnit courseUnit) {
		remove(courseUnit);
	}

	@Override
	public CourseUnit getCourseUnit(Integer id) {
		return super.find(id);
	}

	@Override
	public PagingLoadResult<CourseUnit> getCourseUnitList(
			PagingLoadConfig loadConfig) {
		Search courseUnitSearch = getSearchFromLoadConfig(loadConfig, "id");
		SearchResult<CourseUnit> result = searchAndCount(courseUnitSearch);
		return getPagingLoadResult(loadConfig, result);
	}

	@Override
	public PagingLoadResult<CourseUnit> getCourseUnitList(Integer courseId,
			PagingLoadConfig loadConfig) {
		Search courseUnitSearch = getSearchFromLoadConfig(loadConfig, "id");
		courseUnitSearch.addFilterEqual("course_id", courseId);
		SearchResult<CourseUnit> result = searchAndCount(courseUnitSearch);
		return getPagingLoadResult(loadConfig, result);
	}

	@Override
	public List<CourseUnit> getCourseUnits() {
		Search s = new Search();
		s.addSort("name", false);
		return search(s);
	}

	@Override
	public void saveCourseUnit(CourseUnit courseUnit) {
		save(courseUnit);
	}
}
